<template>
  <div class="app">
    <div id="app">
      <img class="a" :src="img1" alt />
      <img class="b" :src="img2" alt />
      <el-form ref="login" :model="loginForm" :rules="rules" class="loginForm" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" clearable show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from "vuex";
//引入封装好的登录接口
import { userLogin } from "../util/request/api";
export default {
  data() {
    return {
      img1: require("../assets/images/1.png"),
      img2: require("../assets/images/2.png"),
      loginForm: {
        username: "",
        password: ""
      },
      //规则验证
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    ...mapActions(["changeUserAction"]),
    login() {
      this.$refs.login.validate(valid => {
        if (valid) {
          userLogin(this.loginForm).then(res => {
            console.log(res, "登录响应");
            if (res.code == 200) {
              this.changeUserAction(res.list);
              this.$router.push("/index");
              this.$message.success(res.msg);
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          console.log(valid, "err");
        }
      });
    }
  }
};
</script>

<style scoped>
#app {
  width: 100%;
  height: 100vh;
  background-image: linear-gradient(180deg, #ff6040 0%, #ff8a80 100%);
  box-shadow: 0 2px 6px 0 rgba(255, 149, 128, 0.5);
}
.a {
  width: 1047px;
  height:90vh;
}
.b {
  width: 455px;
  height: 120px;
  position: relative;
  top: -600px;
  left: 500px;
}
.el-form {
  width: 450px;
  position: relative;
  right: -480px;
  top: -450px;
}
</style>
